"""基础类型配置

包含Python基础类型到C++类型的映射配置
"""

# 基础类型配置
CONFIG = {
    "int": {
        "cpp_type": "int",
        "includes": [],
        "default_value": "0",
        "is_type": True,
        "module": "builtins",
        "pass_by": "value",  # 基础类型按值传递
        "satisfies_protocols": [
            "Integer",
            "Number",
            "Comparable",
            "Hashable",
        ],  # 满足的 Protocol
        "constructors": {
            "signatures": [
                "int() -> int",
                "int(x: int) -> int",
                "int(x: float) -> int",
                "int(x: str) -> int",
                "int(x: bool) -> int",
            ],
            "templates": [
                "int{}",
                "{x}",
                "static_cast<int>({x})",
                "std::stoi({x})",
                "static_cast<int>({x})",
            ],
            "includes": ["<string>"],
            "priority": 1,
        },
    },
    "float": {
        "cpp_type": "double",
        "includes": [],
        "default_value": "0.0",
        "is_type": True,
        "module": "builtins",
        "pass_by": "value",  # 基础类型按值传递
        "satisfies_protocols": ["Number", "Comparable", "Hashable"],  # 满足的 Protocol
        "constructors": {
            "signatures": [
                "float() -> float",
                "float(x: float) -> float",
                "float(x: int) -> float",
                "float(x: str) -> float",
            ],
            "templates": [
                "double{}",
                "{x}",
                "static_cast<double>({x})",
                "std::stof({x})",
            ],
            "includes": ["<string>"],
            "priority": 1,
        },
    },
    "bool": {
        "cpp_type": "bool",
        "includes": [],
        "default_value": "false",
        "is_type": True,
        "module": "builtins",
        "pass_by": "value",  # 基础类型按值传递
        "satisfies_protocols": ["Hashable"],  # 满足的 Protocol
        "constructors": {
            "signatures": ["bool(x: T) -> bool"],
            "templates": [],  # 空模板，通过 callback 处理
            "includes": [],
            "priority": 0,
            "callback": "mcpy.builtin.bool_callback:BoolFunctionCallback",
        },
    },
    "bytes": {
        "cpp_type": "mc::array<uint8_t>",
        "includes": ["<vector>", "<cstdint>"],
        "default_value": "mc::array<uint8_t>()",
        "is_type": True,
        "module": "builtins",
        "pass_by": "const_ref",  # 大对象按常量引用传递
    },
    "None": {
        "cpp_type": "void",
        "includes": [],
        "is_type": True,
        "module": "builtins",
        "description": "None类型，用于函数返回类型，映射为C++的void",
    },
    "Any": {
        "cpp_type": "mc::variant",
        "includes": ["<mc/variant.h>"],
        "default_value": "mc::variant{}",
        "is_type": True,
        "module": "builtins",
        "pass_by": "const_ref",  # variant 按常量引用传递
    },
    # =========================================================================
    # 协议类型 (Protocol Types)
    # =========================================================================
    # 基于 Python PEP 544 和 collections.abc 的标准协议定义
    # 这些协议可以被 TYPE_VARS 引用，也可以在类型注解中使用
    # stub 系统可以通过实现这些方法让新类型满足协议
    # --- 容器协议 ---
    "Iterable[T]": {
        "is_type": True,
        "is_protocol": True,
        "required_methods": ["__iter__"],
        "cpp_concept": "std::ranges::range",
        "description": "可迭代协议：要求实现 __iter__ 方法",
        "module": "typing",
        "generic_params": ["T"],
    },
    "Iterator[T]": {
        "is_type": True,
        "is_protocol": True,
        "required_methods": ["__iter__", "__next__"],
        "cpp_concept": "std::input_iterator",
        "description": "迭代器协议：要求实现 __iter__ 和 __next__ 方法",
        "module": "typing",
        "generic_params": ["T"],
    },
    "Sequence[T]": {
        "is_type": True,
        "is_protocol": True,
        "required_methods": ["__getitem__", "__len__", "__iter__"],
        "cpp_concept": "std::ranges::random_access_range",
        "description": "序列协议：支持索引访问、长度和迭代",
        "module": "typing",
        "generic_params": ["T"],
    },
    "Mapping[K, V]": {
        "is_type": True,
        "is_protocol": True,
        "required_methods": [
            "__getitem__",
            "__len__",
            "__contains__",
            "keys",
            "values",
            "items",
        ],
        "description": "映射协议：类似 dict 的接口",
        "module": "typing",
        "generic_params": ["K", "V"],
    },
    "Container[T]": {
        "is_type": True,
        "is_protocol": True,
        "required_methods": ["__contains__"],
        "description": "容器协议：支持成员测试 (in 运算符)",
        "module": "typing",
        "generic_params": ["T"],
    },
    "Sized": {
        "is_type": True,
        "is_protocol": True,
        "required_methods": ["__len__"],
        "description": "有大小协议：支持 len() 函数",
        "module": "typing",
    },
    "Reversible[T]": {
        "is_type": True,
        "is_protocol": True,
        "required_methods": ["__reversed__"],
        "cpp_concept": "std::ranges::bidirectional_range",
        "description": "可反转协议：支持 reversed() 函数",
        "module": "typing",
        "generic_params": ["T"],
    },
    # Lambda 函数类型（签名泛型）
    # TypeInfo(
    #   kind=TypeKind.FUNCTION,    # 标识为函数类型
    #   name="lambda",             # 函数类型名称
    #   args=[Arg1, Arg2, ...],    # 参数类型列表（非容器元素！）
    #   return_type=Ret            # 返回类型
    # )
    #
    # 示例：
    #   lambda[[int, Any], int] 表示：
    #     - 参数：(int, Any)
    #     - 返回：int
    #     - Python 侧：完整签名信息
    #     - C++ 侧：mc::variant（类型擦除）
    #
    # ========== C++ 映射策略 ==========
    # 1. 作为变量（堆模式）：mc::variant
    # 2. 作为变量（栈模式）：__lambda_xxx（强类型 functor，优化）
    # 3. 作为容器元素：mc::variant（始终类型擦除）
    #
    # ========== 通用性验证 ==========
    # 此机制适用于所有函数类型：
    # - lambda：匿名函数
    # - function：命名函数（未来扩展）
    # - method：绑定方法（未来扩展）
    "lambda": {
        "is_type": True,
        "kind": "function",  # TypeKind.FUNCTION - 触发签名泛型机制
        "cpp_type": "mc::variant",
        "includes": ["<mc/variant.h>", "<mcpy/builtins/lambda.h>"],
        "description": "Lambda 函数类型（签名泛型：参数和返回类型保存在 TypeInfo 中）",
        "module": "builtins",
    },
    # --- 操作协议 ---
    "Callable": {
        "is_type": True,
        "is_protocol": True,
        "required_methods": ["__call__"],
        "cpp_concept": "std::invocable",
        "description": "可调用协议：可以像函数一样调用",
        "module": "typing",
    },
    "Coroutine": {
        "is_type": True,
        "is_protocol": True,
        "kind": "generic",  # Coroutine[YieldType, SendType, ReturnType]
        "template_params": ["T_co", "T_contra", "V_co"],
        "description": "协程协议：async函数返回的类型",
        "module": "typing",
    },
    "Hashable": {
        "is_type": True,
        "is_protocol": True,
        "required_methods": ["__hash__", "__eq__"],
        "description": "可哈希协议：可用作 dict 键或 set 元素",
        "module": "typing",
    },
    "Comparable": {
        "is_type": True,
        "is_protocol": True,
        "required_methods": [
            "__lt__",
            "__le__",
            "__gt__",
            "__ge__",
            "__eq__",
            "__ne__",
        ],
        "cpp_concept": "std::totally_ordered",
        "description": "可比较协议：支持所有比较运算符",
        "module": "typing",
    },
    # --- 数值协议 ---
    "Integer": {
        "is_type": True,
        "is_protocol": True,
        "required_methods": [
            "__lt__",
            "__le__",
            "__gt__",
            "__ge__",
            "__eq__",
            "__ne__",
            "__add__",
            "__sub__",
            "__mul__",
            "__floordiv__",
            "__mod__",
            "__pow__",
            "__and__",
            "__or__",
            "__xor__",
            "__lshift__",
            "__rshift__",
        ],
        "cpp_concept": "std::integral",
        "description": "整数协议：支持整数运算（可用于 int32_t, int64_t 等）",
        "module": "typing",
    },
    "Number": {
        "is_type": True,
        "is_protocol": True,
        "required_methods": [
            "__lt__",
            "__le__",
            "__gt__",
            "__ge__",
            "__eq__",
            "__ne__",
            "__add__",
            "__sub__",
            "__mul__",
            "__truediv__",
        ],
        "cpp_concept": "std::arithmetic",
        "description": "数值协议：支持基本算术运算",
        "module": "typing",
    },
}
